<?php

class UserController extends BaseController {

    public function doLogin() {
        $messages = array(
            'required' => 'Kolom :attribute tidak boleh kosong.',
        );

        $validator = Validator::make(Input::all(), User::$rules_login, $messages);

        if($validator->fails()) {
            // Redirect into login page
            return Redirect::route('login')->withErrors($validator)->withInput();
        }else{
            // Attempt user login

            $auth = Auth::attempt(array(
                'username' => Input::get('username'),
                'password' => Input::get('password')
                ));

            if($auth) {
                // Redirect ke dashboard                
                return Redirect::route('dashboard');
            }else{
                return Redirect::route('login')->with('message', 'Username atau password salah');
            }
        }
    }

    public function doLogout() {
        Auth::logout();
        return Redirect::route('login');
    }

    // fungsi manipulasi data ----------------------------------------------------------------

    public function index()
    {
        Session::put('selected_menu', 'master');
        $users = User::where('is_active', '=', '1')->where('id', '<>', Auth::user()->id)->orderBy('nama','ASC')->get();

        return View::make('users.index', compact('users'));
    }

    /**
     * Show the form for creating a new user
     *
     * @return Response
     */
    public function create()
    {
        return View::make('users.create');
    }

    /**
     * Store a newly created user in storage.
     *
     * @return Response
     */
    public function store()
    {
        $validator = Validator::make($data = Input::all(), User::$rules);

        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();
        }

        User::create($data);
        // $data = [
        //     "nama" => Input::get('nama'),
        //     "username" => Input::get('username'),
        //     "password" => Input::get('password'),
        //     "email" => Input::get('email')
        // ];

        // if (Input::get('password') == Input::get('verify_password')) {
        //     # code...
        //     // return 'sukses';
        //     $user = new User();
        //     $user->nama = Input::get('nama');
        //     $user->username = Input::get('username');
        //     $user->username = Input::get('username');
        // } else {
        //     return Redirect::back()->withErrors($validator)->withInput();
        // }
        // return $data;
        // return Input::all();        


        return Redirect::route('users.index');
    }

    /**
     * Display the specified user.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $user = User::findOrFail($id);

        return View::make('users.show', compact('user'));
    }

    /**
     * Show the form for editing the specified user.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $user = User::find($id);

        return View::make('users.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        // $validator = Validator::make($data = Input::all(), User::$rules);

        // if ($validator->fails())
        // {
        //     return Redirect::back()->withErrors($validator)->withInput();
        // }

        // User::create($data);

        //---------------------------------------------------------------

        $user = User::findOrFail($id);

        $validator = Validator::make($data = Input::all(), User::$rules);

        if ($validator->fails())
        {
         return Redirect::back()->withErrors($validator)->withInput();
        }

        $user->update($data);

        // $user->is_active = 0;

        // $user->save();

        if($id == Auth::user()->id)
        {
            return Redirect::route('dashboard');            
        } else
        {
            return Redirect::route('users.index');            
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        User::destroy($id);

        // $user = User::findOrFail($id);

        // $user->is_active = 0;
        // $user->user_id = Auth::user()->id;
        // $user->action = 3;

        // $user->save();

        return Redirect::route('users.index');
    }
}